package com.controller;


import com.mapper.NewsMapper;
import com.pojo.News;
import com.pojo.News1;
import com.pojo.User;
import com.service.CrawlerService;
import com.service.CrawlerUpdateServive;
import com.service.NewsService;
import com.service.SchedulingCrawlerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping
public class HelloController {

    private final Logger logger = LoggerFactory.getLogger(SchedulingCrawlerService.class);


    @Autowired
    private NewsService newsService;

    @Autowired
    private CrawlerService crawlerService;
    @Autowired
    private CrawlerUpdateServive crawlerUpdateService;

    @Autowired
    private SchedulingCrawlerService schedulingCrawlerService;

    @Autowired
    private NewsMapper mapper;

    @GetMapping("/hello")
    String he() {
        return "Hello World";
    }


    @GetMapping("/hi")
    String hello() {
//        News1 news1 = crawlerService.crawlerNews("http://nyncj.changde.gov.cn/zhdt/gzdt/cddt/content_735526");
//        System.out.println(news1);
//        mapper.insert(news1);
//       crawlerUpdateService.isUpdate(1);
        List<String> strings = crawlerService.crawlerUrl(3);
        for (int i = strings.size() - 1; i >= 0; i--) {
            News1 news1 = crawlerService.crawlerNews(strings.get(i));
            mapper.insert(news1);
        }

        return "OK!";
    }

    @GetMapping("/lu")
    String findAll() {
        logger.debug("开始爬取-->");
        List<String> stringList = crawlerService.crawlerUrl(10);
        for (int i = stringList.size() - 1; i >= 0; i--) {
            News1 news1 = crawlerService.crawlerNews(stringList.get(i));
            mapper.insert(news1);
        }
        logger.debug("结束爬取-->");
        return "success!";
    }

    @GetMapping("/update")
    public String update() {
        System.out.println("-->开始爬取");
        for (int i = 1; i <= 7; i++) {
            crawlerUpdateService.isUpdate(i);
        }

        System.out.println("-->爬取成功");
        return "Update Success";
    }

    @GetMapping("/updateFile")
    public String updateFile() throws IOException {
        schedulingCrawlerService.tofiles();
        return "Update File Success";
    }


}
